package com.huike.report.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.*;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId = request.getDeptId();
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list = contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId = (Long) datum.get("dept_id");
            if (deptId != null) {
                SysDept dept = deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }

    @Override
    public Map<String, Integer> getVulnerabilityMap(Date beginCreateTime, Date endCreateTime) {
        Map<String, Integer> map = new HashMap<>();
        int cluesNums = clueMapper.selectByDate(beginCreateTime, endCreateTime);
        int effectiveCluesNums = clueMapper.selectByDate1(beginCreateTime, endCreateTime);
        int businessNums = businessMapper.selectByCount(beginCreateTime, endCreateTime);
        int contractNums = contractMapper.selectByCount(beginCreateTime, endCreateTime);
        map.put("cluesNums", cluesNums);
        map.put("effectiveCluesNums", effectiveCluesNums);
        map.put("businessNums", businessNums);
        map.put("contractNums", contractNums);
        return map;
    }

    @Override
    public List<Map<String, Object>> salesStatistic(IndexStatisticsVo indexStatisticsVo) {
        /*Integer all=clueMapper.selectByAAA(indexStatisticsVo);*/
        /*String format1 = DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
        String format2 = DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59");*/

        Integer all = clueMapper.countByClueCreateTime(indexStatisticsVo.getBeginCreateTime(), indexStatisticsVo.getEndCreateTime());
        List<Map<String, Object>> list = clueMapper.assignRecordMapper(indexStatisticsVo);

        for (Map<String, Object> datum : list) {
            String a = (String) datum.get("create_by");

            SysUser sysUser = sysUserMapper.selectUserByUserName(a);
            Long deptId = sysUser.getDeptId();
            SysDept sysDept = deptMapper.selectDeptById(deptId);
            datum.put("deptName", sysDept.getDeptName());
            Long num = (Long) datum.get("num");
            BigDecimal radio = getRadio(all, num);
            datum.put("radio", radio);
        }

        return list;

    }

    @Override
    public IndexVo getAll(String beginCreateTime, String endCreateTime) {
        String username = SecurityUtils.getUsername();
        IndexVo indexVo = new IndexVo();
        int cluesNum=clueMapper.selectByName(beginCreateTime, endCreateTime, username);
        indexVo.setCluesNum(cluesNum);
        int business= businessMapper.selectByName(beginCreateTime, endCreateTime, username);
        indexVo.setBusinessNum(business);
        int a =contractMapper.selectByName(beginCreateTime, endCreateTime, username);
        indexVo.setContractNum(a);
        return indexVo;

    }


    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }

}